package com.zuzhili.database;

import java.util.Iterator;
import java.util.Map;

public class SqlSentenceHelper {
	static public String createReplaceIS(String table){
		return "replace into "+table+" ";
	}
	static public String createReplaceIgnoreIS(String table){
		return "replace or ignore into "+table+" ";
	}
	static public String createInsertIS(String table){
		return "insert into "+table+" ";
	}
	static public String insert(String table,Map<String,String> contents){
		String ret="replace into "+table;
		boolean first=true;
		Iterator it=contents.entrySet().iterator();
		while(it.hasNext()){
			Map.Entry entry=(Map.Entry)it.next();
			if(first){
				first=false;
				ret+=" ("+(String)entry.getKey();
			}else{
				ret+=","+(String)entry.getKey();
			}
		}
		ret+=") values ";
		first=true;
		Iterator it2=contents.entrySet().iterator();
		while(it2.hasNext()){
			Map.Entry entry=(Map.Entry)it2.next();
			String value=(String)entry.getValue();
			value=escape(value);
			if(first){
				first=false;
				ret+="('"+value+"'";
			}else{
				ret+=",'"+value+"'";
			}
		}
		ret+=")";
		return ret;
	}
	static private String escape(String input){
		String ret=null;
		if(input!=null){
			
			ret=input.replace("'", "''");
		}
		
		return ret;
	}
	static public String insert(String table,Map<String,String> contents,String sep){
		String ret="replace into "+table;
		boolean first=true;
		Iterator it=contents.entrySet().iterator();
		while(it.hasNext()){
			Map.Entry entry=(Map.Entry)it.next();
			if(first){
				first=false;
				ret+=" ("+(String)entry.getKey();
			}else{
				ret+=","+(String)entry.getKey();
			}
		}
		ret+=") values ";
		first=true;
		Iterator it2=contents.entrySet().iterator();
		while(it2.hasNext()){
			Map.Entry entry=(Map.Entry)it2.next();
			String value=(String)entry.getValue();
			value=escape(value);
			if(first){
				first=false;
				ret+="("+sep+value+sep;
			}else{
				ret+=","+sep+value+sep;
			}
		}
		ret+=")";
		return ret;
	}
	static public String insert(String type,String table,Map<String,String> contents){
		String ret=type+table;
		boolean first=true;
		Iterator it=contents.entrySet().iterator();
		while(it.hasNext()){
			Map.Entry entry=(Map.Entry)it.next();
			if(first){
				first=false;
				ret+=" ("+(String)entry.getKey();
			}else{
				ret+=","+(String)entry.getKey();
			}
		}
		ret+=") values ";
		first=true;
		Iterator it2=contents.entrySet().iterator();
		while(it2.hasNext()){
			Map.Entry entry=(Map.Entry)it2.next();
			if(first){
				first=false;
				ret+="(\""+(String)entry.getValue()+"\"";
			}else{
				ret+=",\""+(String)entry.getValue()+"\"";
			}
		}
		ret+=")";
		return ret;
	}
	static public String addColum(String target,String col,int type/*0,first,1,end,2,nor*/){
		if(type==0){
			
			target+=" ("+col;
		}else if(type==1){
			
			target+=","+col+")";
		}else if(type==2){
			
			target+=","+col;
		}
		return target;
	}
	static public String addValue(String target,String value,int vtype/*0,num,1,string*/,int type/*0,first,1,end,2,nor*/){
		if(type==0){
			if(vtype==0){
				target+=" values ("+value;
			}else{
				target+=" values (\""+value+"\"";
			}
		}else if(type==1){
			if(vtype==0){
				target+=","+value+")";
			}else{
				target+=",\""+value+"\")";
			}
		}else if(type==2){
			if(vtype==0){
				target+=","+value;
			}else{
				target+=",\""+value+"\"";
			}
		}
		return target;
	}
	static public String addValuePair(String target,String value,int vtype/*0,num,1,string*/,int type/*0,first,1,end,2,nor*/){
		if(type==0){
			if(vtype==0){
				target+=" ("+value;
			}else{
				target+=" (\""+value+"\"";
			}
		}else if(type==1){
			if(vtype==0){
				target+=","+value+")";
			}else{
				target+=",\""+value+"\")";
			}
		}else if(type==2){
			if(vtype==0){
				target+=","+value;
			}else{
				target+=",\""+value+"\"";
			}
		}
		return target;
	}
	
}
